/**
 * 
 */
package kr.co.insoft.core.service.menus.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import kr.co.insoft.framework.config.MenuEntityModel;
import kr.co.insoft.framework.config.Menus;

import org.apache.commons.collections.Predicate;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
public class MenuCompute {

	final List<Menus> treeData = Arrays.asList(Menus.values());
	final HashSet<String> enabledMenus;

	public MenuCompute(HashSet<String> enabledMenus) {
		this.enabledMenus = enabledMenus;
		this.enabledMenus.add("-1"); /* ROOT 메뉴 기본 추가 */
	}

	public MenuEntityModel getTree() {
		MenuEntityModel menus = new MenuEntityModel();
		menus.setMenus(Menus.ROOT);
		menus.setChilds(getChild(treeData, Menus.ROOT));
		return menus;
	}

	@SuppressWarnings("unchecked")
	private List<MenuEntityModel> getChild(List<Menus> tree, final Menus menu) {
		ArrayList<Menus> findGroups = new ArrayList<Menus>();
		findGroups = new ArrayList<Menus>(
				org.apache.commons.collections.CollectionUtils.select(tree,
						new Predicate() {
							public boolean evaluate(Object obj) {
								Menus childMenu = ((Menus) obj);
								if (enabledMenus.contains(Integer
										.toString(childMenu.ordinal())))
									return childMenu.getParanetMenuId() == menu
											.ordinal();
								else
									return false;
							}
						}));
		List<MenuEntityModel> result = new ArrayList<MenuEntityModel>();
		MenuEntityModel tmpmenu = null;
		for (Menus item : findGroups) {
			tmpmenu = new MenuEntityModel();
			tmpmenu.setMenus(item);
			tmpmenu.setChilds(getChild(treeData, item));
			result.add(tmpmenu);
		}
		return result;
	}
}
